//
//  JRVisiableView.swift
//  swiftWeiBo
//
//  Created by Johnray on 15/9/21.
//  Copyright © 2015年 Johnray. All rights reserved.
//

import UIKit

class JRVisiableView: UIView {
    
    //点击按钮回调闭包
//    var loginClick:(()->())?
//    
//    var registerClick:(()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        //fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        
        setUpUI()
        
    }
    
    func setUIinfo(imageName:String?,title:String?){
    
        if imageName != nil && title != nil{
            coverImageView.hidden = true
            
            HomeImageView.removeFromSuperview()
            
            IconimageView.image = UIImage(named: imageName!)
            
            label.text = title!
        
        }else{
            
            makeIconCircle()
        
        }
    
    
    }
    
    //MARK: - 旋转icon
    private func makeIconCircle(){
        let anima = CABasicAnimation(keyPath: "transform.rotation")
        
        anima.toValue = 2 * M_PI
        anima.repeatCount = MAXFLOAT
        
        anima.duration = 15
        //动画不会被销毁
        anima.removedOnCompletion = false
    
        IconimageView.layer.addAnimation(anima, forKey: nil)
    
    }
    
    /**
    设置UI
    */
    private func setUpUI(){
    
        addSubview(IconimageView)
        
        addSubview(coverImageView)
        
        addSubview(HomeImageView)
        
        addSubview(label)
        
        addSubview(registerBtn)
        
        addSubview(loginBtn)
        
        
        self.backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        
        
        
        
        for v in subviews{
            v.translatesAutoresizingMaskIntoConstraints = false
        
        }
        
        //约束图标
        addConstraint(NSLayoutConstraint(item: IconimageView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: IconimageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -60))
        
        //约束小房子
        addConstraint(NSLayoutConstraint(item: HomeImageView, attribute: .CenterX, relatedBy: .Equal, toItem: IconimageView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: HomeImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: IconimageView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        //约束label
        addConstraint(NSLayoutConstraint(item: label, attribute: .Top, relatedBy: .Equal, toItem: IconimageView, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: label, attribute: .CenterX, relatedBy: .Equal, toItem: IconimageView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: label, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: label, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //约束注册按钮
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Left, relatedBy: .Equal, toItem: label, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: label, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //约束登陆按钮
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Right, relatedBy: .Equal, toItem: label, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: label, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        
        //约束遮挡的imageview
        addConstraint(NSLayoutConstraint(item: coverImageView, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverImageView, attribute: .Bottom, relatedBy: .Equal, toItem: registerBtn, attribute: .Bottom, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverImageView, attribute: .Width, relatedBy: .Equal, toItem: self, attribute: .Width, multiplier: 1.0, constant: 0))
    
    }
    
//    //MARK: - 按钮点击回调方法
//    func registerBtnClick(){
//        
//        registerClick?()
//        
//    }
//    
//    func loginBtnClick(){
//    
//        loginClick?()
//    }
    
    //MARK: - 添加控件
    lazy private var IconimageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    lazy private var HomeImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    lazy private var label:UILabel = {
        let l = UILabel()
        
        l.textColor = UIColor.darkGrayColor()
        
        l.font = UIFont.systemFontOfSize(14)
        
        l.sizeToFit()
        
        l.numberOfLines = 0
        
        l.text = "关注一些人，回这里看看有什么惊喜"
        
        l.textAlignment = NSTextAlignment.Center
        
        return l
    
    }()
    
    lazy var registerBtn:UIButton = {
        let btn = UIButton()
    
        btn.setTitle("注册", forState: UIControlState.Normal)
        
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        btn.setTitleColor(UIColor.blueColor(), forState: UIControlState.Highlighted)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Highlighted)
        //设置按钮字体
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        //btn.addTarget(self, action: "registerBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
    lazy var loginBtn:UIButton = {
        let btn = UIButton()
        
        btn.setTitle("登陆", forState: UIControlState.Normal)
        
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        btn.setTitleColor(UIColor.blueColor(), forState: UIControlState.Highlighted)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Highlighted)
        
        //btn.addTarget(self, action: "loginBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
        }()
    
     lazy private var coverImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))

        
    

}
